var contentDiv = document.getElementById('score_div');
var participantsDiv = document.getElementById('participants_div');
var friends = {};

// Note that if you click the button several times in succession,
// if the state update hasn't gone through, it will submit the same
// delta again.  The hangout data state only remembers the most-recent
// update.
function buttonClicked1() {
	addVote(1);
}

function buttonClicked2() {
	addVote(2);
}

function buttonClicked3() {
	addVote(3);
}

function buttonClicked4() {
	addVote(4);
}

function buttonClicked5() {
	addVote(5);
}

function addVote(value){
  	console.log("Button clicked");
   	console.log( gapi.hangout.data.getState()['vote'] );
   	var vote = "";

	if ( gapi.hangout.data.getState()['vote'] ) {
		vote = gapi.hangout.data.getState()['vote'];
	}

	var id = gapi.hangout.getParticipantId();
	var name = gapi.hangout.getParticipantById(id).displayName;
	
	

	var parsed = vote.split(";");
	var repeatIndex = -1;	
	var i;
	for (i = 0; i < parsed.length; i = i + 1){

		var unit = parsed[i].split(":");
		var thatname = unit[0];
		if (thatname == name) {
			repeatIndex = i;
		} 		
	}
	
	if (repeatIndex == -1) {
		repeatIndex = parsed.length; 
	}
	
	parsed[repeatIndex] = '' + name + ":" + value;
	var newDict = parsed.join(";");
	if (newDict.substring(0,1) == ';'){
		newDict = newDict.substring(1);
	}
		
	gapi.hangout.data.submitDelta({'vote': newDict});
}

function addPersonLabel(name, vote) {
	var stars ="";
	for(var i = 0; i < vote; i++){
		stars += "<img src='http://siweihack.appspot.com/static/css/img/star.png'></img>";
	}

	if( $("#"+name).length === 0){
		console.log($(this) , "not exist");
		friends[name] = vote;
		$("#friends").append("<div id='"+name +"'><span class='label warning'>"+ name +"</span><span data-num='"+ vote +"'class='stars'>"+stars+"</span></div>");
	}else{
		console.log($(this) , "exist");
	    $("#"+name).html("<span class='label warning'>"+ name +"</span><span data-num='"+ vote +"'class='stars'>"+stars+"</span>");
	
	}
}
       
function participantsUpdated( participantsArray ) {
	console.log("participants: " + participantsArray);
	participantsDiv.innerHTML = "Participants: " + participantsArray.length;
}

// Sets up callbacks for state change
// You should not set up the state object until you get your first callback.
function init() {
   console.log("init");
   gapi.hangout.data.addStateChangeListener(stateUpdated);
   gapi.hangout.addParticipantsListener(participantsUpdated);
}

// Whenever the shared data is updated, rewrite UI
function stateUpdated(delta, metadata) {
	if(!gapi.hangout.data.getState()['vote']) {
		//document.getElementById("score_div").innerHTML = "The count is ???."
	} else {
		var vote = gapi.hangout.data.getState()['vote'];
		var parsed = vote.split(";");
		var i = 0;
		var total = 0;
		var count = 0;
		$("#friends").html("");
		for (i = 0; i < parsed.length; i = i + 1){
			
			var pa = parsed[i].split(":");
			addPersonLabel(pa[0], pa[1]);
			total = total + parseInt(pa[1]);
			count = count + 1;
		}
		
		var avg = 0;
		if (count > 0) {
			avg = total / count;
		}
		avg = "" + avg;
		//document.getElementById("score_div").innerHTML = "The score is " + avg + ".";
		
	}
}

// Note that the hangouts object is not set up until the gadget loads
gadgets.util.registerOnLoadHandler(init);
       
// Reset value of "count" to 0
function resetCounter(){      
	gapi.hangout.data.submitDelta({'vote': ""});
}
